﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework.Input.Touch;
using Spinning.Spinning.Interface;

namespace Spinning.Spinning.Scenes.SinglePlayer
{
    class SinglePlayerMenuScene : BaseScene, ISaveLoad
    {
        List<StageIcon> icons = new List<StageIcon>();
        public override void OnLoad()
        {
            base.OnLoad();
            Sprite backGroundSprite = new Sprite();
            backGroundSprite.textureName = "bg";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            Sprite itemTitleSprite = new Sprite();
            itemTitleSprite.textureName = "singlePlayerMenu/txt_level";
            itemTitleSprite.ZIndex = Constants.ui;
            itemTitleSprite.OnLoad();
            itemTitleSprite.Position = new Vector2(170, 60);
            this.Attach(itemTitleSprite);

            Sprite barSprite = new Sprite();
            barSprite.textureName = "item_store/bar";
            barSprite.ZIndex = Constants.uibackground;
            barSprite.OnLoad();
            barSprite.Position = new Vector2(75, 40);
            this.Attach(barSprite);

            Sprite blockSprite = new Sprite();
            blockSprite.textureName = "singlePlayerMenu/block_05";
            blockSprite.ZIndex = Constants.background;
            blockSprite.OnLoad();
            blockSprite.Position = new Vector2(27, 180);
            this.Attach(blockSprite);

            ButtonSprite backSprite = new ButtonSprite();
            backSprite.textureName = "p2/back";
            backSprite.ZIndex = Constants.ui;
            backSprite.Position = new Vector2(385, 695);
            backSprite.OnLoad();
            TouchManager.RegisteTouchArea(backSprite);
            this.Attach(backSprite);
            backSprite.OnTouchEvent += OnBackTouch;
            SortChildren();

            TextEntity te = new TextEntity();
            ResourceManager.ContentRoot = "";
            te.fontString = "Century Gothic";
            te.OnLoad();
            te.Origin = new Vector2(0.5f);
            te.Position = new Vector2(200, 50);
            te.Color = Color.Blue;
            te.Alpha = 0;
            te.ZIndex = Constants.debugText;
            Attach(te);


            for (int j = 0; j < 9; j++)
            {
                StageIcon icon = new StageIcon();
                icon.Position = new Vector2(55 + j % 3 * 115, 220 + (int)(j / 3) * 138);
                icon.StageNumber = j + 1;
                icon.OnLoad();
                icon.isLock = true;
                TouchManager.RegisteTouchArea(icon);
                Attach(icon);
                icon.OnTouchEvent += OnIconTouch;
                if (j < 4)
                    te.Value += j + ":" + icon.isLock;
                icons.Add(icon);
            }
            icons[0].isLock = false;
        }

        void OnIconTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            StageIcon ic = (StageIcon)entity;
            switch (ic.StageNumber)
            {
                case 1:
                    if (Game1.stageManager.GameSrate.isLockedGameStage[1] == false)
                    {
                        Game1.stageManager.GoToScene("lifeStage1");
                    }
                    else 
                    {
                        Game1.stageManager.GoToScene("animation");
                    }
                    
                    break;
                case 2:
                    Game1.stageManager.GoToScene("animationBoss1");
                    //Game1.stageManager.GoToScene("lifeStage1Boss");
                    break;
                case 3:
                    Game1.stageManager.GoToScene("lifeStage2");
                    break;
                case 4:
                    Game1.stageManager.GoToScene("animationBoss2");
                        //Game1.stageManager.GoToScene("lifeStage2Boss");
                    break;
            }
        }

        void OnBackTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GoToScene("role");
        }

        public override BaseScene Clone()
        {
            SinglePlayerMenuScene scene = new SinglePlayerMenuScene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.Load(Game1.stageManager.GameSrate);
            scene.SortChildren();
            return scene;
        }

        public void Save(Unitity.Storage.SaveData state)
        {

        }

        public void Load(Unitity.Storage.SaveData state)
        {
            for (int i = 0; i < icons.Count; i++)
            {
                StageIcon ic = icons[i];
                if (i < state.isLockedGameStage.Count)
                    ic.isLock = state.isLockedGameStage[i];
                else {
                    state.isLockedGameStage.Add(ic.isLock);
                }
            }
        }
    }
}
